<script setup>
import { ref } from "vue";
import { View, Delete, Upload, Download } from '@element-plus/icons-vue'
import LeftPanel from "../components/FlowDesign/FlowForm/form-design/left-panel.vue";
import ContentPanel from "../components/FlowDesign/FlowForm/form-design/content-panel.vue";
import RightPanel from "../components/FlowDesign/FlowForm/form-design/right-panel.vue";
import PreviewForm from "../components/FlowDesign/FlowForm/form-design/modals/preview-form.vue";
const list = ref([]);
const currentItem = ref({});
import { useStore } from "vuex";
const previewVisible = ref(false);
const store = useStore();
const currentIndex = ref(0)
const dialogVisible = ref(false);
const currentTitle = ref("");
const headerData = ref([
  { title: "预览", icon: "View" },
  { title: "清空", icon: "Delete" },
  { title: "导入JSON", icon: "Upload" },
  { title: "生成JSON", icon: "Download" },
]);

// 获取操作按钮图标
const getActionIcon = (index) => {
  const icons = {
    0: View,
    1: Delete,
    2: Upload,
    3: Download
  };
  return icons[index];
};

// 点击功能
const itemClick = (index) => {
  if (index === 0) {
    previewVisible.value = true;
    return;
  }
  if (index === 1) {
    currentItem.value = {};
    store.dispatch("form/updateConfig", []);
    return;
  }
  currentIndex.value = index;
  dialogVisible.value = true;
};

/* 事件处理区域 */
const currentItemClick = (item) => {
  console.log("item:", JSON.stringify(item));
  currentItem.value = item;
};
</script>

<template>
  <div class="form-designer">
    <header class="designer-header">
      <div class="header-title">
        <span class="title-text">表单设计器</span>
      </div>
      <div class="header-actions">
        <el-button
          v-for="(item, index) in headerData"
          :key="item.title"
          type="primary"
          :class="['action-btn', index === 0 ? 'preview-btn' : '']"
          @click="itemClick(index)"
        >
          <el-icon class="action-icon" v-if="getActionIcon(index)">
            <component :is="getActionIcon(index)" />
          </el-icon>
          <span>{{ item.title }}</span>
        </el-button>
      </div>
    </header>

    <section class="designer-content">
      <LeftPanel class="left-panel" @click-add="add" />
      <ContentPanel
        class="content-panel"
        :list="list"
        @current-item-click="currentItemClick"
        :add-current-index="currentIndex"
      />
      <RightPanel class="right-panel" :current-item="currentItem" />
    </section>

    <!-- 预览表单设计 -->
    <PreviewForm :visible="previewVisible" @cancel="previewVisible = false" />
  </div>
</template>

<style lang="scss" scoped>
.form-designer {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  background-color: #f5f7fa;
}

.designer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 64px;
  background: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 10;

  .header-title {
    .title-text {
      font-size: 18px;
      font-weight: 600;
      color: #303133;
      background: linear-gradient(45deg, #409EFF, #36D1DC);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
  }

  .header-actions {
    display: flex;
    gap: 12px;

    .action-btn {
      display: flex;
      align-items: center;
      gap: 6px;
      padding: 8px 16px;
      border-radius: 6px;
      transition: all 0.3s ease;
      background: #f5f7fa;
      border: none;
      color: #606266;

      &:hover {
        background: #ecf5ff;
        color: #409EFF;
        transform: translateY(-1px);
      }

      &.preview-btn {
        background: #409EFF;
        color: #fff;

        &:hover {
          background: #66b1ff;
          transform: translateY(-1px);
        }
      }

      .action-icon {
        font-size: 16px;
      }
    }
  }
}

.designer-content {
  display: flex;
  flex: 1;
  height: calc(100vh - 64px);
  padding: 20px;
  gap: 20px;
  overflow: hidden;

  .left-panel {
    width: 280px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
    }
  }

  .content-panel {
    flex: 1;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
    overflow-y: auto;
  }

  .right-panel {
    width: 300px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
    }
  }
}

// 滚动条样式
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-thumb {
  background: #dcdfe6;
  border-radius: 3px;
}

::-webkit-scrollbar-track {
  background: #f5f7fa;
  border-radius: 3px;
}

// 响应式布局
@media screen and (max-width: 1400px) {
  .designer-content {
    .left-panel {
      width: 250px;
    }
    
    .right-panel {
      width: 280px;
    }
  }
}

@media screen and (max-width: 1200px) {
  .designer-content {
    .left-panel {
      width: 220px;
    }
    
    .right-panel {
      width: 260px;
    }
  }
}

@media screen and (max-width: 768px) {
  .designer-header {
    padding: 0 16px;
    height: 56px;

    .header-title .title-text {
      font-size: 16px;
    }

    .header-actions .action-btn {
      padding: 6px 12px;
      font-size: 13px;
    }
  }

  .designer-content {
    padding: 12px;
    gap: 12px;
  }
}
</style>
